package com.xbfinal.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.reggie.common.R;
import com.xbfinal.reggie.dto.DishDto;
import com.xbfinal.reggie.mapper.DishMapper;
import com.xbfinal.reggie.pojo.Dish;
import com.xbfinal.reggie.pojo.DishFlavor;
import com.xbfinal.reggie.service.DishFlavorService;
import com.xbfinal.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
implements DishService {

    @Autowired
     DishFlavorService dishFlavorService;
    /**
     * 新增菜品的同时保存口味的数据
     * 同时操作多张表 所以需要事务控制
     * 要是事务 @Transactional生效得在启动类开启事务的支持
     * @EnableTransactionManagement
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //先保存菜品的基本信息到菜品表
        this.save(dishDto);
        //菜品id
        final Long id = dishDto.getId();
        //保存菜品口味表
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map(item->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());


        //保存菜品口味数据到菜品口味表dish_favor
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 根据菜品id查询菜品基本信息和对应的口味
     * @param id
     * @return
     */
    @Override
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {
        //1.查菜品基本
        final Dish dish = this.getById(id);
        final DishDto dishDto = new DishDto();
        //1.5使用对象拷贝
        BeanUtils.copyProperties(dish,dishDto);
        //2.查口味信息,从dish_flavor查
            //2.1条件构造器
        LambdaQueryWrapper<DishFlavor> QueryWrapper
                =new LambdaQueryWrapper<>();
            //2.2条件查询
        QueryWrapper.eq(DishFlavor::getDishId,dish.getId());

        final List<DishFlavor> list
                = dishFlavorService.list(QueryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish
        this.updateById(dishDto);

        //先清理dish_flavor
         LambdaQueryWrapper<DishFlavor> LambdaQueryWrapper
                 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(LambdaQueryWrapper);
        //添加dish_flavor
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors=flavors.stream().map(item->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 删除菜品信息，同时删除对应的口味信息
     * @param  ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids){
        //删除菜品信息
        this.removeByIds(ids);
        //查询对应的口味信息的id
        LambdaQueryWrapper<DishFlavor> LambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        for (Long id:ids) {
            LambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(LambdaQueryWrapper);
        }

    }
}
